package com.example.yeujian5.yuejian5.activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.example.yeujian5.yuejian5.Listenner.ActivityListener;
import com.example.yeujian5.yuejian5.Listenner.ClubDetailListener;
import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.bean.ClubBean;
import com.example.yeujian5.yuejian5.fragment.ClubClassFragment;
import com.example.yeujian5.yuejian5.fragment.ClubInfoFragment;
import com.example.yeujian5.yuejian5.fragment.ClubVideoFragment;
import com.example.yeujian5.yuejian5.model.ClubModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClubDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mClub;
    private LinearLayout mClass;
    private LinearLayout mVideo;
    private Fragment mTab6;
    private Fragment mTab7;
    private Fragment mTab8;
    private ClubBean info;
    private Integer ClubInfo;
    private String id, userid;
    private TextView name;
    private ImageView pic, club_collect;
    private ImageView back;
    private String club_name, thumb;
    ClubDetailListener clubDetailListener = new ClubDetailListener() {
        @Override
        public void onResponse(ClubBean listBean) {
            info = listBean;
            initData();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        final Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id");
        userid = getSharedPreferences("Userid", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        Log.v("获取用户id", String.valueOf(userid));
        ClubModel model = new ClubModel();
        model.getdetail(id, clubDetailListener);
        initViews();
        initEvents();
        setSelect(0);
    }

    public void initViews() {
        mClub = (LinearLayout) findViewById(R.id.clubinfo_club);
        mClass = (LinearLayout) findViewById(R.id.clubinfo_class);
        mVideo = (LinearLayout) findViewById(R.id.clubinfo_video);
        name = (TextView) findViewById(R.id.cname);
        pic = (ImageView) findViewById(R.id.cpic);
        back = (ImageView) findViewById(R.id.back);
        club_collect = (ImageView) findViewById(R.id.club_col);
    }

    public void initEvents() {
        mClub.setOnClickListener(this);
        mClass.setOnClickListener(this);
        mVideo.setOnClickListener(this);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        club_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
    }


    private void collect() {
//        ClubModel clubModel = new ClubModel();
//        Call<Integer> call = clubModel.CollectClub(userid,id);
//        call.enqueue(new Callback<Integer>() {
//
//            @Override
//            public void onResponse(Call<Integer> call, Response<Integer> response) {
//                if(response.body()!=0){
//                    club_collect.setImageResource(R.drawable.ic_heart);
//                    Toast.makeText(ClubDetailActivity.this,"收藏成功",Toast.LENGTH_LONG).show();
//                }else {
//                    club_collect.setImageResource(R.drawable.ic_heart1);
//                    Toast.makeText(ClubDetailActivity.this,"取消成功！",Toast.LENGTH_LONG).show();
//                }
//            }
//
//            @Override
//            public void onFailure(Call<Integer> call, Throwable t) {
//
//            }
//        });
    }



    private void getClubState() {
        ClubModel userModel = new ClubModel();
        Call<Integer> call = userModel.getClubState(id, userid);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() == 0) {
                    club_collect.setImageResource(R.drawable.ic_heart);
                } else {
                    club_collect.setImageResource(R.drawable.ic_heart1);
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }

    public void initData() {
        club_name = info.getClub_name();
        thumb = info.getThumb();

        name.setText(club_name);
        if (thumb != "") {
            Picasso.with(ClubDetailActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/" + thumb)
                    .into(pic);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            pic.setImageResource(R.drawable.qya);
        }
        getClubState();
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
                if (mTab6 == null) {
                    mTab6 = new ClubInfoFragment();
                    transaction.add(R.id.id_content1, mTab6);
                } else {
                    transaction.show(mTab6);
                }
                break;
            case 1:
                if (mTab7 == null) {
                    mTab7 = new ClubClassFragment()
                    ;
                    transaction.add(R.id.id_content1, mTab7);
                } else {
                    transaction.show(mTab7);

                }
                break;
            case 2:
                if (mTab8 == null) {
                    mTab8 = new ClubVideoFragment();
                    transaction.add(R.id.id_content1, mTab8);
                } else {
                    transaction.show(mTab8);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab6 != null) {
            transaction.hide(mTab6);
        }
        if (mTab7 != null) {
            transaction.hide(mTab7);
        }
        if (mTab8 != null) {
            transaction.hide(mTab8);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clubinfo_club:
                setSelect(0);
                break;
            case R.id.clubinfo_class:
                setSelect(1);
                break;
            case R.id.clubinfo_video:
                setSelect(2);
                break;

            default:
                break;
        }
    }
}
